package com.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduate.common.CustomException;
import com.graduate.dto.GoodsDto;
import com.graduate.entity.Goods;
import com.graduate.entity.GoodsNote;
import com.graduate.mapper.GoodsMapper;
import com.graduate.service.GoodsNoteService;
import com.graduate.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {
    @Autowired
    private GoodsNoteService goodsNoteService;

    //新增商品同时保存对应的note数据
    @Transactional
    public void saveWithNote(GoodsDto goodsDto) {
        //保存商品的基本信息到商品表
        this.save(goodsDto);

        Long goodsId = goodsDto.getId();//商品id

        List<GoodsNote> flavors = goodsDto.getFlavors();

        flavors = flavors.stream().map((item)->{
            item.setGoodsId(goodsId);
            return item;
        }).collect(Collectors.toList());

        //保存商品note到goodsNote表中
        goodsNoteService.saveBatch(flavors);
    }

    //根据id查询商品信息和note信息
    @Override
    public GoodsDto getByIdWithNote(Long id) {
        //查询商品基本信息，从goods中查询
        Goods goods = this.getById(id);

        GoodsDto goodsDto = new GoodsDto();
        BeanUtils.copyProperties(goods,goodsDto);

        //查询当前商品对应的note信息
        LambdaQueryWrapper<GoodsNote> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GoodsNote::getGoodsId,goods.getId());
        List<GoodsNote> note = goodsNoteService.list(queryWrapper);
        goodsDto.setFlavors(note);

        return goodsDto;
    }

    //修改商品信息
    @Override
    @Transactional
    public void updateWithNote(GoodsDto goodsDto) {
        //更新goods基本信息
        this.updateById(goodsDto);
        //清理当前商品的note信息
        LambdaQueryWrapper<GoodsNote> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(GoodsNote::getGoodsId,goodsDto.getId());
        goodsNoteService.remove(queryWrapper);
        //添加当前提交过来的note数据
        List<GoodsNote> note = goodsDto.getFlavors();
        note = note.stream().map((item)->{
            item.setGoodsId(goodsDto.getId());
            return item;
        }).collect(Collectors.toList());
        goodsNoteService.saveBatch(note);
    }


    //批量删除商品信息
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //构造条件查询器
        LambdaQueryWrapper<Goods> queryWrapper = new LambdaQueryWrapper<>();
        //先查询该s商品是否在售卖，如果是则抛出业务异常
        queryWrapper.in(ids!=null,Goods::getId,ids);
        List<Goods> list = this.list(queryWrapper);
        for (Goods dish : list) {
            Integer status = dish.getStatus();
            //如果不是在售卖,则可以删除
            if (status == 0){
                this.removeById(dish.getId());
            }else {
                //此时应该回滚,因为可能前面的删除了，但是后面的是正在售卖
                throw new CustomException("删除菜品中有正在售卖菜品,无法全部删除");
            }
        }
    }




}
